/**
 * [省市区指令]
 * @param  {[type]} $compile [description]
 * @param  {Array}  rest)    {               return {        restrict: 'E',        template: '',        replace: true,        compile: function pwdCompilingFunction(tElm, tAttrs) {            return function pwdLinkingFunction($scope, $elem, $attrs) {                $scope._provinceSelected [description]
 * @return {[type]}          [description]
 */
angular.module('app').directive('uiPcd', function($compile, rest) {
    return {
        restrict: 'E',
        template: '',
        replace: true,
        compile: function pwdCompilingFunction(tElm, tAttrs) {
            return function pwdLinkingFunction($scope, $elem, $attrs) {
                $scope._provinceSelected = [], $scope._citySelected = [], $scope._areaSelected = [];
                $scope.province = [], $scope.city = [], $scope.area = [];
                var childClass = $attrs.childClass;
                var provinceTPL = '<div class="' + childClass + '"><ui-select ng-model="_provinceSelected.selected" theme="bootstrap"><ui-select-match placeholder="请选择省份...">{{$select.selected.name}}</ui-select-match><ui-select-choices repeat="item in province | filter: $select.search"><div ng-bind-html="item.name | highlight: $select.search"></div></ui-select-choices></ui-select></div>';
                var cityTPL = '<div class="' + childClass + '"><ui-select ng-model="_citySelected.selected" theme="bootstrap"><ui-select-match placeholder="请选择城市...">{{$select.selected.name}}</ui-select-match><ui-select-choices repeat="item in city | filter: $select.search"><div ng-bind-html="item.name | highlight: $select.search"></div></ui-select-choices></ui-select></div>';
                var areaTPL = '<div class="' + childClass + '"><ui-select ng-model="_areaSelected.selected" theme="bootstrap"><ui-select-match placeholder="请选择区域...">{{$select.selected.name}}</ui-select-match><ui-select-choices repeat="item in area | filter: $select.search"><div ng-bind-html="item.name | highlight: $select.search"></div></ui-select-choices></ui-select></div>';
                var newElem = $compile(provinceTPL + cityTPL + areaTPL)($scope);
                $elem.append(newElem);
                $scope.params.parentId = 0;
                rest.post({
                    methodId: '4010',
                    data: $scope.params,
                    success: function(results, status, res) {
                        var data = [];
                        if (results.code == "0") {
                            data = results.res.data;
                        }
                        $scope.province = data;
                        if ($attrs.provinceid) {
                            $scope.province.forEach(function(ele, index) {
                                if (ele.id == $attrs.provinceid) {
                                    $scope._provinceSelected.selected = ele;
                                    $scope.city = ele.data;
                                }
                            });
                        }
                        if ($attrs.provinceid&&$attrs.cityid) {
                            $scope.city.forEach(function(ele, index) {
                                if (ele.id == $attrs.cityid) {
                                    $scope._citySelected.selected = ele;
                                    $scope.area = ele.data;
                                }
                            });
                        }
                        if($attrs.cityid&&$attrs.areaid){
                            $scope.area.forEach(function(ele, index) {
                                if (ele.id == $attrs.areaid) {
                                    $scope._areaSelected.selected = ele;
                                }
                            });
                        }
                        $scope.$digest();
                    }
                });
                $scope.$watch('$attrs',function(newValue,oldValue, $$scope){
                  if($scope.newValue != $attrs.oldValue){
                    $scope.newValue = $attrs.oldValue;
                  }
                })
                $scope.$watch('_provinceSelected.selected', function(_newSelected, _oldSelected, $$scope) {
                    $scope._citySelected.selected = $scope._citySelected.selected || undefined;
                    if (!!_newSelected) {
                        $scope.city = Object.prototype.toString.call(_newSelected.data) === '[object Object]' ? [_newSelected.data] : _newSelected.data;
                        $scope._provinceSelected.selected;
                    }
                })
                $scope.$watch('_citySelected.selected', function(_newSelected, _oldSelected, $$scope) {
                    $scope._areaSelected.selected = $scope._areaSelected.selected || undefined;
                    if (!!_newSelected) {
                        $scope.area = Object.prototype.toString.call(_newSelected.data) === '[object Object]' ? [_newSelected.data] : _newSelected.data;
                    }
                })
            }
        }
    }
});
